package com.lwj.diao.handlers;

import com.lwj.diao.Htmls;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.util.Map;

public class ExecutorHandler implements HttpHandler {
    final String mResultTemplateHtml = Htmls.HTML_EXECUTOR.toString();

    @Override
    public void handle(HttpExchange exchange) throws IOException {
        try {
            Map<String, String> params = Htmls.convertStream2Params(exchange);
            String source = params.get("java_source");
            String resultHtml = Htmls.parseExecuteResult(this.mResultTemplateHtml, source, true);
            byte[] finalHtmlBytes = resultHtml.getBytes(Htmls.UTF8_CS);
            exchange.sendResponseHeaders(200, finalHtmlBytes.length);
            exchange.getResponseBody().write(finalHtmlBytes);
            exchange.getResponseBody().close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
